package com.zjbti.countducks;

import androidx.appcompat.app.AppCompatActivity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private MyBroadcastReceiverOne one;
    private MyBroadcastReceiverTwo two;
    private MyBroadcastReceiverThree three;
    private MyBroadcastReceiverFour four;
    private MyBroadcastReceiverFive five;
    private MyBroadcastReceiverSix six;

    private ImageView iv_horn;
    private TextView tv_left_content,tv_one,tv_two,tv_three,tv_four,tv_five,tv_six;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        registerReceiver();
        init();
    }

    private void init() {
        iv_horn=findViewById(R.id.iv_horn);
        tv_left_content=findViewById(R.id.tv_left_content);
        tv_one=findViewById(R.id.tv_one);
        tv_two=findViewById(R.id.tv_two);
        tv_three=findViewById(R.id.tv_three);
        tv_four=findViewById(R.id.tv_four);
        tv_five=findViewById(R.id.tv_five);
        tv_six=findViewById(R.id.tv_six);
        iv_horn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tv_left_content.setVisibility(View.VISIBLE);
                iv_horn.setClickable(false);
                Intent intent=new Intent();
                intent.setAction("Count_Ducks");
                sendOrderedBroadcast(intent,null);
            }
        });
    }

    private int num=0;
    private class MyBroadcastReceiverOne extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            tv_one.setVisibility(View.VISIBLE);
            num=num+1;
            tv_one.setText(num+" ");
            Log.i("BroadcastReceiverOne","广播接收者One,接收到了广播消息");
            delay();
        }
    }

    private class MyBroadcastReceiverTwo extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            tv_two.setVisibility(View.VISIBLE);
            num=num+1;
            tv_two.setText(num+" ");
            Log.i("BroadcastReceiverTwo","广播接收者Two,接收到了广播消息");
            delay();
        }
    }

    private class MyBroadcastReceiverThree extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            tv_three.setVisibility(View.VISIBLE);
            num=num+1;
            tv_three.setText(num+" ");
            Log.i("BroadcastReceiverThree","广播接收者Three,接收到了广播消息");
            delay();
        }
    }

    private class MyBroadcastReceiverFour extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            tv_four.setVisibility(View.VISIBLE);
            num=num+1;
            tv_four.setText(num+" ");
            Log.i("BroadcastReceiverFour","广播接收者Four,接收到了广播消息");
            delay();
        }
    }

    private class MyBroadcastReceiverFive extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            tv_five.setVisibility(View.VISIBLE);
            num=num+1;
            tv_five.setText(num+" ");
            Log.i("BroadcastReceiverFive","广播接收者Five,接收到了广播消息");
            delay();
        }
    }

    private class MyBroadcastReceiverSix extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            tv_six.setVisibility(View.VISIBLE);
            num=num+1;
            tv_six.setText(num+" ");
            Log.i("BroadcastReceiverSix","广播接收者Six,接收到了广播消息");
            delay();
        }
    }

    //延迟500毫秒
    private void delay() {
        try {
            Thread.sleep(500);
        }catch (InterruptedException e){
            e.printStackTrace();
        }
    }

    private void registerReceiver(){
        //动态注册MyBroadcastReceiverOne广播接收者
        one=new MyBroadcastReceiverOne();
        IntentFilter filter1=new IntentFilter();
        filter1.setPriority(1000);
        filter1.addAction("Count_Ducks");
        registerReceiver(one,filter1);
        //动态注册MyBroadcastReceiverTwo广播接收者
        two=new MyBroadcastReceiverTwo();
        IntentFilter filter2=new IntentFilter();
        filter2.setPriority(800);
        filter2.addAction("Count_Ducks");
        registerReceiver(two,filter2);
        //动态注册MyBroadcastReceiverThree广播接收者
        three=new MyBroadcastReceiverThree();
        IntentFilter filter3=new IntentFilter();
        filter3.setPriority(600);
        filter3.addAction("Count_Ducks");
        registerReceiver(three,filter3);

        //动态注册MyBroadcastReceiverOne广播接收者
        four=new MyBroadcastReceiverFour();
        IntentFilter filter4=new IntentFilter();
        filter4.setPriority(400);
        filter4.addAction("Count_Ducks");
        registerReceiver(four,filter4);
        //动态注册MyBroadcastReceiverTwo广播接收者
        five=new MyBroadcastReceiverFive();
        IntentFilter filter5=new IntentFilter();
        filter5.setPriority(200);
        filter5.addAction("Count_Ducks");
        registerReceiver(five,filter5);
        //动态注册MyBroadcastReceiverThree广播接收者
        six=new MyBroadcastReceiverSix();
        IntentFilter filter6=new IntentFilter();
        filter6.setPriority(100);
        filter6.addAction("Count_Ducks");
        registerReceiver(six,filter6);
    }

   @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(one);
        unregisterReceiver(two);
        unregisterReceiver(three);
       unregisterReceiver(four);
       unregisterReceiver(five);
       unregisterReceiver(six);
    }
}